package com.denlaku.longan.controller.impl;

import com.denlaku.longan.anno.Audit;
import com.denlaku.longan.anno.VarBy;
import com.denlaku.longan.audit.AuditVariables;
import com.denlaku.longan.controller.TenantMemberController;
import com.denlaku.longan.core.Current;
import com.denlaku.longan.http.Resp;
import com.denlaku.longan.http.Return;
import com.denlaku.longan.qo.Query;
import com.denlaku.longan.service.TenantMemberService;
import com.denlaku.longan.service.UserService;
import com.denlaku.longan.vo.PageBy;
import com.denlaku.longan.vo.TenantMember;
import com.denlaku.longan.vo.User;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author tianx
 */
@RestController
public class TenantMemberControllerImpl implements TenantMemberController {
    @Autowired
    private TenantMemberService tenantMemberService;

    @Autowired
    private UserService userService;

    @VarBy
    @Override
    public Return<List<TenantMember>> pageList(TenantMember tenantMember, PageBy pageBy) {
        PageHelper.startPage(pageBy.getPage(), pageBy.getSize());
        List<TenantMember> list = tenantMemberService.list(tenantMember);
        Set<Long> userIds = list.stream().map(TenantMember::getUserId).collect(Collectors.toSet());
        Map<Long, User> userMap = userService.listBaseInfoMap(userIds);
        return Resp.success(list).setUmap(userMap);
    }

    @Override
    public Return<TenantMember> get(Long userId) {
        Long tenantId = Current.getTenantId();
        TenantMember tenantMember = tenantMemberService.get(tenantId, userId);
        return Resp.success(tenantMember);
    }

    @Override
    @Audit(module = "TenantMember", operation = "audit.operation.addTenantMember",
            message = "disabled tenant member: {#userAccount}")
    public Return<Long> add(TenantMember tenantMember) {
        setAuditVariables(tenantMember);
        tenantMemberService.add(tenantMember);
        return Resp.success(tenantMember.getId());
    }

    private void setAuditVariables(TenantMember tenantMember) {
        Long userId = tenantMember.getUserId();
        User user = userService.get(userId);
        String userAccount = Optional.ofNullable(user).map(User::getAccount).orElse(String.valueOf(userId));
        AuditVariables.set("userAccount", userAccount);
    }

    @Override
    @Audit(module = "TenantMember", operation = "audit.operation.updateTenantMember",
            message = "update tenant member: {#userAccount}")
    public Return<Void> update(TenantMember tenantMember) {
        setAuditVariables(tenantMember);
        tenantMemberService.update(tenantMember);
        return Resp.success();
    }

    @Override
    @Audit(module = "TenantMember", operation = "audit.operation.deleteTenantMember",
            message = "delete tenant member: {#userAccount}")
    public Return<Void> delete(TenantMember tenantMember) {
        setAuditVariables(tenantMember);
        tenantMemberService.delete(Query.of(tenantMember.getId()));
        return Resp.success();
    }

}
